package com.pcbly.web.components.utils.template.dwr;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.pcbly.web.bean.crm.AccountDzdBean;
import com.pcbly.web.components.utils.ClassUtilsStatic;
import com.pcbly.web.components.utils.template.AbstractModel;
import com.pcbly.web.components.utils.template.Constant;
import com.pcbly.web.components.utils.template.action.ModelActionXml;

public class MergeApplicationDwr {

	public MergeApplicationDwr() {
		// TODO Auto-generated constructor stub
	}

	private Class<?> clz;

	public Class<?> getClz() {
		return clz;
	}

	public void setClz(Class<?> clz) {
		this.clz = clz;
	}

	public void execute() {
		try {
			String packageString = clz.getPackage().getName();
			// System.out.println(packageString);
			String path = packageString.replaceAll("\\.", "/");
			String pacha = clz.getPackage().getName();
			File file = new File("src/" + path);
			if (file.isDirectory()) {
				String[] files = file.list(); 
				StringBuilder sbCreate = new StringBuilder();
				StringBuilder sbConvert = new StringBuilder();
				for (String tfile : files) {
					if (tfile.indexOf("java") >= 0) {
						String te = tfile.substring(0, tfile.indexOf("."));
						te = pacha + "." + te;
						Class<?> temp = ClassUtilsStatic.forName(te);
						AbstractModel model = new ModelActionXml(temp);
						String xmlPath = model.dwrXmlPath();
						// System.out.println(xmlPath);

						File fileXML = new File(xmlPath);
						if (fileXML.exists()) {
							String content = readTpl(fileXML);
							content = reduceBeans(content);  
							sbCreate.append(getCreate(content));
							sbConvert.append(getConvert(content));
						}
//						break;
					}
				}
				packageString = packageApplicationStrutsPath();
				file = new File(packageString);
				String xml = readTpl(file);
//				System.out.print(xml);
				xml = replace(xml, sbCreate.toString(),"\\#\\{create\\}");
				xml = replace(xml, sbConvert.toString(),"\\#\\{convert\\}");
				packageString = applicationStrutsPath();
				file = new File(packageString);

				FileUtils.writeStringToFile(file, xml, "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 
	protected String getCreate(String content){
		content=content.substring(0,content.lastIndexOf("create>")+7);
		return content;
	}
	protected String getConvert(String content){
		content=content.substring(content.lastIndexOf("create>")+7,content.lastIndexOf("convert>")+8);
		return content;
	}
	protected String applicationStrutsPath() {
		String name = "applicationContext-dwr.xml";
		String packagePath = clz.getPackage().getName();
		packagePath = packagePath.substring(packagePath.lastIndexOf(".") + 1,
				packagePath.length());
		return Constant
				.getFilePath("com.pcbly.web.action." + packagePath, name);
	}

	protected String replace(String source, String content,String replace) {
		content = source.replaceAll(replace, content);
		return content;
	}

	protected String packageApplicationStrutsPath() {
		String name = "PackageApplicationDwr.txt";
		return Constant.getFilePath(Constant.packName, name);
	}

	private String reduceBeans(String content) {
		content = content.replaceAll("</beans>", "");
		content = content.substring(content.indexOf(">") + 1, content.length());
		content = content.substring(content.indexOf(">") + 1, content.length());
		content = content.substring(content.indexOf(">") + 1, content.length());
		return content;
	}

	public String readTpl(File tpl) {
		return readTpl(tpl, "UTF-8");
	}

	public String readTpl(File tpl, String charset) {
		String content = null;
		try {
			content = FileUtils.readFileToString(tpl, charset);
		} catch (IOException e) {
			System.out.println("read file faild. " + e.getMessage());
		}
		return content;
	}

	public static void main(String[] args) {
		System.out.println("Begin.....................");
		MergeApplicationDwr model = new MergeApplicationDwr();
		model.setClz(AccountDzdBean.class);
		model.execute();
		System.out.println("End.....................");
	}
}
